package com.superbytecode.sample.redis.service;

import com.superbytecode.redis.annotation.Lock;
import com.superbytecode.sample.redis.entity.User;
import org.springframework.stereotype.Service;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: superbytecode-beacon
 * @Package: com.superbytecode.sample.service
 * @ClassName: RedisLockService
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2023/11/21 16:26
 * @Version: V1.0
 * @Copyright: 2023 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class RedisLockService {

    @Lock("#key")
    public int singleKeyLock(String key) throws InterruptedException {
        Thread.sleep(3000);
        return 1;
    }

    /**
     * 锁两个参数
     *
     * @param key1 参数1
     * @param key2 参数2
     */
    @Lock({"#key", "#key2"})
    public int multipleKeyLock(String key1, String key2) throws InterruptedException {
        Thread.sleep(3000);
        return 1;
    }

    /**
     * 锁入参对象的属性，此处演示锁商品的id
     * 并展示设置等待时间和自定义超时异常信息功能
     *
     * @param user 商品
     */
    @Lock(key = "#user.id", waitTime = 3, errorMsg = "'等待超过'+#waitTime+'秒，系统繁忙，请稍后再试。'")
    public int objectKey(User user) throws InterruptedException {
        // TODO: 2023/11/21 执行业务
        // 业务处理时间大于waitTime时，报错
        Thread.sleep(5);
        return 0;
    }
}